import { app, BrowserWindow, ipcMain } from 'electron'
import { electronApp, optimizer } from '@electron-toolkit/utils'
import ipcMainEvents from './ipcMainEvents'
ipcMain.on('window-create', (options) => {
  ipcMainEvents.createWindow(options)
})
ipcMain.on('child-window-create', (_, url: string, options) => {
  ipcMainEvents.createChildWindow(url, options)
})

app.whenReady().then(() => {
  electronApp.setAppUserModelId('com.electron')

  app.on('browser-window-created', (_, window) => {
    optimizer.watchWindowShortcuts(window)
  })

  ipcMain.on('ping', () => console.log('pong'))

  ipcMainEvents.createWindow()

  app.on('activate', function () {
    // On macOS it's common to re-create a window in the app when the
    if (BrowserWindow.getAllWindows().length === 0) ipcMainEvents.createWindow()
  })
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})
